package com.xr.org.emp;

import java.text.SimpleDateFormat;
 

import com.xr.common.CommUtil;
import com.xr.common.Tools;
import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * myWorkDiaryViewModel
 */
public class myWorkDiaryViewModel extends DefaultViewModel {
  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {

		DBStatement dbs = new DBStatement();
		Dataset dsWd = getDataset("datasetWorkDiary");
		
		 //CommUtil.dbsUpdateData(dbs, dsWd, "EMP_WORK_DIARY", "EMP_WORK_DIARY_ID");
		 SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
	 
	 Record record=dsWd.getCurrent();
	switch (record.getState()) {
	   case Record.STATE_NEW:
	     dbs.setSql("insert into EMP_WORK_DIARY(EMP_WORK_DIARY_ID,EMPLOYEE_ID,ONOFF_STATION_ID,SHORT_CONTEXT,WORK_DATE,START_DATE,END_DATE,WORK_CONTENT,PROGRESS,WORK_HOURS,CREATE_ID)values(:EMP_WORK_DIARY_ID,:EMPLOYEE_ID,:ONOFF_STATION_ID,:SHORT_CONTEXT,:WORK_DATE,:START_DATE,:END_DATE,:WORK_CONTENT,:PROGRESS,:WORK_HOURS,:CREATE_ID)");
	     dbs.parameters().setValue("EMP_WORK_DIARY_ID", CommUtil.getNewId("general"));
	     dbs.parameters().setValue("EMPLOYEE_ID",CommUtil.getEmployeeId());
	     dbs.parameters().setValue("ONOFF_STATION_ID",record.getValue("ONOFF_STATION_ID"));
	     dbs.parameters().setValue("SHORT_CONTEXT",record.getValue("SHORT_CONTEXT"));
	     dbs.parameters().setDate("WORK_DATE",record.getDate("WORK_DATE"));
	     
	     dbs.parameters().setDataType("START_DATE", 12);
	     dbs.parameters().setDate("START_DATE",java.sql.Timestamp.valueOf(Tools.format(record.getDate("WORK_DATE"),"yyyy-MM-dd")+" "+Tools.format(record.getDate("START_DATE"),"HH:mm:ss")));
	     dbs.parameters().setDataType("END_DATE", 12);
	     dbs.parameters().setValue("END_DATE",  java.sql.Timestamp.valueOf(Tools.format(record.getDate("WORK_DATE"),"yyyy-MM-dd")+" "+Tools.format(record.getDate("END_DATE"),"HH:mm:ss")) );
	     
	     dbs.parameters().setValue("WORK_CONTENT",record.getValue("WORK_CONTENT"));
	     dbs.parameters().setValue("PROGRESS",record.getValue("PROGRESS"));
	     dbs.parameters().setValue("WORK_HOURS",record.getValue("WORK_HOURS"));
	     dbs.parameters().setValue("CREATE_ID",CommUtil.getEmployeeId()); 
	     dbs.execute();
	     dbs.setSql("update emp_work_diary set work_hours= 24*(end_date-start_date) where EMP_WORK_DIARY_ID="+dbs.parameters().getValue("EMP_WORK_DIARY_ID"));
	     dbs.execute();
	     break;
	   case Record.STATE_MODIFIED:
	     dbs.setSql("update EMP_WORK_DIARY set SHORT_CONTEXT=:SHORT_CONTEXT,WORK_DATE=:WORK_DATE,START_DATE=:START_DATE,END_DATE=:END_DATE,WORK_CONTENT=:WORK_CONTENT,PROGRESS=:PROGRESS,WORK_HOURS=:WORK_HOURS,MODIFY_DATE=:MODIFY_DATE where EMP_WORK_DIARY_ID=:EMP_WORK_DIARY_ID");
	     dbs.parameters().setValue("EMP_WORK_DIARY_ID",record.getValue("EMP_WORK_DIARY_ID"));
	     dbs.parameters().setValue("EMPLOYEE_ID",CommUtil.getEmployeeId());
	     dbs.parameters().setValue("ONOFF_STATION_ID",record.getValue("ONOFF_STATION_ID"));
	     dbs.parameters().setValue("SHORT_CONTEXT",record.getValue("SHORT_CONTEXT"));
	     dbs.parameters().setDate("WORK_DATE",record.getDate("WORK_DATE"));
	     
	     dbs.parameters().setDataType("START_DATE", 12);
	     dbs.parameters().setDate("START_DATE",java.sql.Timestamp.valueOf(Tools.format(record.getDate("WORK_DATE"),"yyyy-MM-dd")+" "+Tools.format(record.getDate("START_DATE"),"HH:mm:ss")));
	     dbs.parameters().setDataType("END_DATE", 12);
	     dbs.parameters().setValue("END_DATE",  java.sql.Timestamp.valueOf(Tools.format(record.getDate("WORK_DATE"),"yyyy-MM-dd")+" "+Tools.format(record.getDate("END_DATE"),"HH:mm:ss")) );
	     
	     
	     dbs.parameters().setDataType("MODIFY_DATE", 12);
	     dbs.parameters().setValue("MODIFY_DATE", java.sql.Timestamp.valueOf(Tools.format(new java.util.Date(),"yyyy-MM-dd HH:mm:ss")) );
	     
	     dbs.parameters().setValue("WORK_CONTENT",record.getValue("WORK_CONTENT"));
	     dbs.parameters().setValue("PROGRESS",record.getValue("PROGRESS"));
	     dbs.parameters().setValue("WORK_HOURS",record.getValue("WORK_HOURS"));
	     dbs.execute();
	     dbs.setSql("update emp_work_diary set work_hours= 24*(end_date-start_date) where EMP_WORK_DIARY_ID="+record.getValue("EMP_WORK_DIARY_ID"));
	     dbs.execute();
	     break;
	   case Record.STATE_DELETED:
		   dbs.setSql("update EMP_WORK_DIARY set dr=1 where EMP_WORK_DIARY_ID=:EMP_WORK_DIARY_ID");
		     dbs.parameters().setValue("EMP_WORK_DIARY_ID", record.getValue("EMP_WORK_DIARY_ID"));
		     dbs.execute();
	     break;
	   default:
	     
	 }
	 dbs.close();
	 
		 
		 
    //super.doUpdateData(parameters, outParameters);
  }
  
  

}
